<?php

function get_kategori_berita_nama_from_id($id) {
    $sql = "SELECT kategori_nama FROM kategoris WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_nama'];
}

function get_berita_isi_from_id($id) {
    $sql = "SELECT berita_isi FROM beritas WHERE berita_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    echo replace_url_img_from_admin_to_public($baris['berita_isi']);
}

function get_kategori_produk_nama_from_id($id) {
    $sql = "SELECT kategori_nama FROM kategori_produks WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_nama'];
}

function get_kategori_produk_seo_from_id($id) {
    $sql = "SELECT kategori_seo FROM kategori_produks WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_seo'];
}

function get_kategori_produk_id_from_seo($seo) {
    $sql = "SELECT kategori_id FROM kategori_produks WHERE kategori_seo='$seo' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_id'];
}

function get_kategori_galeri_nama_from_id($id) {
    $sql = "SELECT kategori_nama FROM kategori_galeris WHERE kategori_id='$id' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['kategori_nama'];
}

function get_produk_gambar($kode) {
    $sql = "SELECT produk_gambar FROM produks WHERE produk_id='$kode' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['produk_gambar'];
}

function get_galeri_gambar($kode) {
    $sql = "SELECT galeri_gambar FROM galeris WHERE galeri_id='$kode' LIMIT 1";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    return $baris['galeri_gambar'];
}

function load_slide_show($kategori, $jumlah) {
    $sql = "SELECT galeri_nama, galeri_gambar FROM galeris WHERE galeri_kategori_id='$kategori' AND galeri_status='1' ORDER BY galeri_id DESC LIMIT " . $jumlah;
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_row($hasil);

    echo "\n <div class=\"slider\">";
    echo "\n  <ul class=\"items\">";
    do {
        list($galeri_nama, $galeri_gambar) = $baris;
        echo "\n <li><img src=\"" . URLHOST . "media/galeri/" . $galeri_gambar . "\" alt=\"" . $galeri_nama . "\" /></li>";
    } while ($baris = mysql_fetch_row($hasil));
    echo "\n  </ul>";
    echo "\n </div>";
}

function load_slide_show_link($kategori, $jumlah) {
    $sql = "SELECT galeri_nama, galeri_gambar FROM galeris WHERE galeri_kategori_id='$kategori' AND galeri_status='1' ORDER BY galeri_id DESC LIMIT " . $jumlah;
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_row($hasil);

    echo "\n <ul class=\"pags\">";
    do {
        list($galeri_nama, $galeri_gambar) = $baris;
        echo "\n <li><a href=\"#\"><strong></strong></a></li>";
    } while ($baris = mysql_fetch_row($hasil));
    echo "\n </ul>";
}

function load_portofolio($jumlah) {
    $sql = "SELECT galeri_nama, galeri_gambar FROM galeris WHERE galeri_kategori_id='2' ORDER BY galeri_id DESC LIMIT " . $jumlah;
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    do {
        list($galeri_nama, $galeri_gambar) = $baris;
        //<li><a href="portfolio.html"><img src="images/gallery-1.jpg" alt=""></a><a href="portfolio.html"><img src="images/gallery-6.jpg" alt=""></a></li>
        echo "\n <li><a href=\"" . URLHOST . "portfolio.php\"><img src=\"" . URLHOST . "media/galeri/" . $galeri_gambar . "\" alt=\"" . $galeri_nama . "\" width=\"200\" height=\"99\" /></a></li>";
    } while ($baris = mysql_fetch_row($hasil));
}

function load_portofolio_semua() {
    $sql = "SELECT galeri_nama, galeri_gambar FROM galeris WHERE galeri_kategori_id='2' ORDER BY galeri_id DESC";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    echo "\n <li>";
    do {
        list($galeri_nama, $galeri_gambar) = $baris;
        echo "\n  <a class=\"fancybox\" rel=\"gallery\" href=\"" . URLHOST . "media/galeri/" . $galeri_gambar . "\" title=\">> " . $galeri_nama . "\">";
        echo "\n   <img src=\"" . URLHOST . "media/galeri/small_" . $galeri_gambar . "\" alt=\"" . $galeri_nama . "\" width=\"180\" height=\"99\" /></a>";
    } while ($baris = mysql_fetch_row($hasil));
    echo "\n </li>";
}

function load_kategori() {
    $sql = "SELECT kategori_nama, kategori_seo FROM kategori_produks order by kategori_nama";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    do {
        list($kategori_nama, $kategori_seo) = $baris;
        echo "\n <li><a href=\"" . URLHOST . "kategori.php?url=" . $kategori_seo . "\" alt=\"" . $kategori_nama . "\" />$kategori_nama</a></li>";
    } while ($baris = mysql_fetch_row($hasil));
}

function load_produk($kategori, $jumlah) {
    if ($jumlah != NULL) {
        $limit = " LIMIT " . $jumlah;
    } else {
        $limit = " ";
    }

    if ($kategori != NULL) {
        $sql = "SELECT produk_gambar, produk_nama, produk_deskripsi FROM produks WHERE produk_kategori_id='$kategori' ORDER BY produk_id DESC " . $limit;
    } else {
        $sql = "SELECT produk_gambar, produk_nama, produk_deskripsi FROM produks ORDER BY produk_id DESC " . $limit;
    }

    //echo $sql;
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    do {
        list($produk_gambar, $produk_nama, $produk_deskripsi) = $baris;
        //echo "test " . $produk_gambar . " aa";
        echo "\n <div class=\"wrap\">";
        echo "\n  <img src=\"" . URLHOST . "media/produk/" . $produk_gambar . "\" alt=\"" . $produk_nama . "\" width=\"253\" height=\"189\" class=\"img-border img-indent\">";
        echo "\n  <div class=\"extra-wrap\">";
        echo "\n   <p class=\"p3\"><strong>$produk_nama</strong></p>";
        echo "\n   <p class=\"p4\"><i>$produk_deskripsi</i></p>";
        echo "\n  </div>";
        echo "\n </div>";
        echo "\n <br>";
        echo "\n <div class=\"clear h2\"></div>";
        echo "\n <br>";
    } while ($baris = mysql_fetch_row($hasil));
}

function load_produk_by_kategori($kategori, $awalan) {
    $p_kategori_seo = get_kategori_produk_seo_from_id($kategori);

    if ($awalan == NULL) {
        $awal = 0;
    } else {
        $awal = $awalan;
    }

    $sql = "SELECT produk_nama, produk_seo, produk_deskripsi, produk_gambar FROM produks WHERE produk_kategori_id='" . $kategori . "' ORDER BY produk_id DESC LIMIT $awal,4";
    $hasil = mysql_query($sql);
    $baris = mysql_fetch_array($hasil);

    if ($baris == false)
        load_data_tidak_ada();
    else {
        do {
            list($produk_nama, $produk_deskripsi, $produk_gambar) = $baris;
            echo "\n <div class = \"wrap\">";
            echo "\n  <img src = \"" . URLHOST . "media/produk/" . $produk_gambar . "\" alt = \"" . $produk_nama . "\" width=\"253\" height = \"189\" class = \"img-border img-indent\">";
            echo "\n  <div class=\"extra-wrap\">";
            echo "\n   <p class=\"p4\"><i>$produk_deskripsi</i></p>";
            echo "\n  </div>";
            echo "\n </div>";
            echo "\n <br>";
            echo "\n <div class=\"clear h2\"></div>";
            echo "\n <br>";
        } while ($baris = mysql_fetch_row($hasil));

        $sql_hal = "SELECT produk_id FROM produks WHERE produk_kategori_id='" . $kategori . "'";
        $hasil_hal = mysql_query($sql_hal);
        $baris_hal = mysql_num_rows($hasil_hal);
        $i = $baris_hal / 5;
        $i = ceil($i);

        echo "\n <div id=\"page\">";
        echo "\n <p><strong>Page : </strong>";
        for ($j = 1; $j <= $i; $j++) {
            $awal = (($j - 1) * 4 + $j) - 1;
//echo "\n <a href=\"".$_SERVER['PHP_SELF']."?cid=$kategori&amp;awal=$awal&amp;halaman=$j\">[$j]</a>";
            echo "\n <a href=\"" . URLHOST . "?url=$p_kategori_seo&amp;awal=$awal&amp;halaman=$j\">[$j]</a>";
        }
        echo "\n </p>";
        echo "\n </div>";
    }
}

function load_news($kategori, $awalans, $halamans) {
    $perhalaman = $halamans;
    $halaman = isset($awalans) ? $awalans : "0";

    $awal = $halaman * $perhalaman;

    $sql_produk = "SELECT berita_id, berita_kategori_id, berita_judul, berita_seo, berita_isi, berita_tgl FROM beritas WHERE berita_kategori_id='" . $kategori . "' ORDER BY berita_id DESC LIMIT $awal, $perhalaman";
    //echo $sql_produk;
    $sql_jumlah = "SELECT count(berita_id) FROM beritas ";
    $links = URLHOST . "news.php?halaman=";

    // produk
    $hasil_produk = mysql_query($sql_produk);
    $baris_produk = mysql_fetch_row($hasil_produk);

    //jumlah produk
    $hasil_jumlah = mysql_query($sql_jumlah);
    $baris_jumlah = mysql_fetch_row($hasil_jumlah);
    $total_halaman = ceil($baris_jumlah[0] / $perhalaman);
    $halaman_out = "";

    // jika berada pada halaman ketiga atau lebih, tampilkan link pertama
    if ($halaman > 1)
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . "0\">&#171; First</a></li>";
    // jika berada pada halaman kedua atau lebih, tampilakn link sebelumnya
    if ($halaman > 0) {
        $hal = $halaman - 1;
        $halaman_out .= "\n <li class=\"previous-off\"><a href=\"" . $links . $hal . "\">< Previous</a></li>";
    }
    // ambil semua nomor halaman
    for ($i = 0; $i < $total_halaman; $i++) {
        $hal = $i + 1;
        if ($i == $halaman) {
            // jika halaman aktif, tidak usah diberi link 
            $halaman_out .= "\n <li class=\"active\">$hal</li>";
        } else {
            $halaman_out .= "\n <li><a href=\"" . $links . $i . "\">$hal</a></li>";
        }
    }
    // jika bukan berada pada halaman terakhir 
    if ($halaman < ($total_halaman - 1)) {
        $hal = $halaman + 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Next ></a></li>";
    }
    // jika bukan pada 2 halaman terakhir 
    if ($halaman < ($total_halaman - 2)) {
        $hal = $total_halaman - 1;
        $halaman_out .= "\n <li class=\"next\"><a href=\"" . $links . $hal . "\">Last &#187;</a></li>";
    }
    // bentuk format halaman 
    $halaman_out = "<div id=\"ki_page\"><ul>$halaman_out</ul></div>";


    if ($baris_produk == false) {
        echo "\n <li>";
        echo "\n <span class=\"extra-wrap\" style=\"padding:5px; text-align:center;\">Data yang anda cari tidak ditemukan</span>";
        echo "\n </li>";
    } else {
        do {
            list($berita_id, $berita_kategori_id, $berita_judul, $berita_seo, $berita_isi, $berita_tgl) = $baris_produk;
            //echo "berita_tgl: " . $berita_tgl;
            $tgl = get_tanggal_from_my($berita_tgl);
            $bulan = get_bulan_from_my($berita_tgl);
            //echo "berita_tgl: " . $tgl . $bulan;
            echo "\n  <li>";
            echo "\n <span class=\"date\"><span>$bulan</span>$tgl</span>";
            echo "\n <span class=\"extra-wrap\">";
            echo replace_url_img_from_admin_to_public($berita_isi);
            //echo "\n Kali ini kami sedang melakukan pencetakan banner outdoor dengan mesin Printing Digital";
            echo "\n </span><h2 class=\"h2\"></h2>";
            echo "\n  </li>";
        } while ($baris_produk = mysql_fetch_row($hasil_produk));
    }

    echo "$halaman_out";
}

// fungsi jika data posting/kategori tidak ada
function load_data_tidak_ada() {
    echo "\n <div class=\"konten\">";
    echo "\n <h1><font color=\"red\">Peringatan</font></h1>";
    echo "\n <p>Data yang Anda cari tidak ditemukan</p>";
    echo "\n </div>";
}

?>
